package Calculator;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Calculator extends JFrame implements ActionListener {
	JTextField tf;
	JButton arr2[] = new JButton[19];
	JMenuItem mnitem;
	double lastNumber;
	String lastOperator;

	public Calculator(String title) {
		super(title);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
		JPanel jp = new JPanel(new FlowLayout());
		JMenuBar mnb = new JMenuBar();
		setJMenuBar(mnb);
		JMenu mnFile = new JMenu("File");
		 mnitem = new JMenuItem("About");
		mnFile.add(mnitem);
		mnb.add(mnFile);
		
		
		JMenu mnHelp = new JMenu("Help");
		mnb.add(mnHelp);
		JPanel jp1 = new JPanel();
		tf = new JTextField(20);
		jp1.add(tf);
		jp.add(jp1);
		JPanel jp2 = new JPanel(new FlowLayout());

		jp2.add(arr2[16] = new JButton("Backspace"));
		jp2.add(arr2[17] = new JButton("CE"));
		jp2.add(arr2[18] = new JButton("C"));

		jp.add(jp2);
		JPanel jp3 = new JPanel(new GridLayout(4, 4));
		for (int i = 7; i <= 9; i++) {
			jp3.add(arr2[i] = new JButton("" + i));
		}
		jp3.add(arr2[15] = new JButton("+"));
		for (int i = 4; i <= 6; i++) {
			jp3.add(arr2[i] = new JButton("" + i));
		}
		jp3.add(arr2[14] = new JButton("-"));
		for (int i = 1; i <= 3; i++) {
			jp3.add(arr2[i] = new JButton("" + i));
		}
		jp3.add(arr2[13] = new JButton("*"));
		jp3.add(arr2[0] = new JButton("0"));
		jp3.add(arr2[11] = new JButton("."));
		jp3.add(arr2[10] = new JButton("="));
		jp3.add(arr2[12] = new JButton("/"));

		jp.add(jp3);
		add(jp);
		for (int i = 0; i < arr2.length; i++) {
			arr2[i].addActionListener(this);
		}
		mnitem.addActionListener(this);

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		for(int i = 0;i<arr2.length;i++){
			if(e.getSource() == arr2[i]){
				switch(i){
				case 0-9: addDigitToDisplay(i);break;
				case 10: processEquals();break;
				case 11-15: processOperator("+");break;
				}
			}
			
				
			}
		if(e.getSource() == mnitem){
			this.aboutWindow();
		}
		
			

	}
	void processOperator(String op) {
		double numberToDisplay = Double.parseDouble(tf.getText());
		tf.setText("");
		lastNumber = numberToDisplay;
		

		lastOperator = op;

	}

	void addDigitToDisplay(int digit) {
		String inputString = tf.getText();
		if (inputString.indexOf("0") == 0)
			inputString = inputString.substring(1);
		if(!inputString.equals("0") || (digit > 0)){
			
		
		tf.setText(inputString + digit);
		}

	}
	
	void processEquals() {
		double result = 0;
		try {
			double numberInDisplay = Double.parseDouble(tf.getText());
			if (lastOperator.equals("/")) {
				if (numberInDisplay == 0)
					throw (new ArithmeticException());
				result = lastNumber / numberInDisplay;
			}
			if (lastOperator.equals("*"))
				result = lastNumber * numberInDisplay;
			if (lastOperator.equals("-"))
				result = lastNumber - numberInDisplay;
			if (lastOperator.equals("+"))
				result = lastNumber + numberInDisplay;
			tf.setText(Double.toString(result));
		} catch (ArithmeticException e) {
			tf.setText("Cannot divide by zero");

		}
		lastOperator = "0";
	}

	void aboutWindow() {
		JFrame jf = new JFrame("About Calculator");
		JPanel pn = new JPanel(new BorderLayout());
		JLabel lb1 = new JLabel("Calculator version HienCute");
		JLabel lb2 = new JLabel("Co Gang Len");
		pn.add(lb1,BorderLayout.NORTH);
		pn.add(lb2,BorderLayout.SOUTH);
		jf.add(pn);
		jf.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		jf.setLocationRelativeTo(null);
		jf.pack();
		jf.setVisible(true);
		
		

	}

	public static void main(String[] args) {
		Calculator input = new Calculator("Java Calculator");
		input.setSize(250, 250);

	}
}
